﻿using System;
using System.Linq;
using EPiBoost.Core.Infrastructure.Interfaces.Providers;
using EPiBoost.Core.Infrastructure.Providers;
using EPiServer.PlugIn;

namespace EPiBoost.Core.Plugins.Gui.Admin
{
    [GuiPlugIn(DisplayName = "EPiBoost - Exception Viewer", Description = "Shows the latest Exceptions",
			Area = PlugInArea.AdminMenu, Url = RegisterResources.FilePathExceptionViewer)]
    public partial class ExceptionViewer : System.Web.UI.Page
    {
	    protected void Page_Load(object sender, EventArgs e)
	    {
		    IExceptionLogProvider logger = new ExceptionLogProvider();
		    var logs = logger.GetLogs().OrderByDescending(x => x.LogDate).ToList();
		    rptErrors.DataSource = logs;
		    rptErrors.DataBind();

				if (Request["details"] != null)
				{
					var detailTicks = long.Parse(Request["details"]);
					var errorItem = logs.FirstOrDefault(x => x.LogDate.Ticks == detailTicks);
					if (errorItem != null)
					{
						ltlDetailUrl.Text = errorItem.Uri;
						ltlHost.Text = errorItem.Host;
						ltlReferrer.Text = errorItem.UrlReferrer;
						ltlError.Text = errorItem.Exception.Replace("\n", "<br/>");

						plhDetails.Visible = true;
					}

				}


	    }
    }
}